package com.ygo.feihua;
import java.util.List;
import java.util.Map;
import android.widget.BaseAdapter;
import android.content.Context;
import android.widget.TextView;
import android.widget.Button;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.widget.EditText;
import com.ygo.feihua.BmobTable.GongGao;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.exception.BmobException;
import com.ygo.feihua.util.*;

public class Adaptergg extends BaseAdapter
{

	List<Map<String,Object>> data;
	Zujian zujian;
	Context context;
	Gongju gj;
	DialogUtils du;
	public Adaptergg(Context context,List<Map<String,Object>> data){
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
	}

	class Zujian{
		TextView gg_bt,gg_nr;
		Button gg_delgg;

	}
	public void addgg(Map<String,Object> map){
		data.add(map);
		notifyDataSetChanged();
	}
	public void delgg(int po){
		data.remove(po);
		notifyDataSetChanged();
	}

	@Override
	public int getCount()
	{
		// TODO: Implement this method
		return data.size();
	}

	@Override
	public Object getItem(int p1)
	{
		// TODO: Implement this method
		return data.get(p1);
	}

	@Override
	public long getItemId(int p1)
	{
		// TODO: Implement this method
		return p1;
	}

	@Override
	public View getView(final int p1, View p2, ViewGroup p3)
	{
		// TODO: Implement this method
		if(p2==null){
			zujian=new Zujian();
			p2=LayoutInflater.from(context).inflate(R.layout.item_gg,null);
			zujian.gg_bt=(TextView) p2.findViewById(R.id.gg_bt);
			zujian.gg_nr=(TextView) p2.findViewById(R.id.gg_nr);
			zujian.gg_delgg=(Button) p2.findViewById(R.id.gg_delgg);
			p2.setTag(zujian);
		}else{
			zujian=(Adaptergg.Zujian) p2.getTag();
		}
		zujian.gg_bt.setText((String)data.get(p1).get("bt"));
		zujian.gg_nr.setText((String)data.get(p1).get("nr"));
		zujian.gg_bt.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View vv)
				{
					View[] v=du.dialoge(true,"修改公告标题","请输入标题");
					final EditText ed=(EditText) v[0];
					Button bt=(Button) v[1];
					ed.setText((String)data.get(p1).get("bt"));
					bt.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View vv)
							{
								du.dialogj1(false,"","加载中,请稍等");

								String bt=ed.getText().toString().trim();
								GongGao gg=new GongGao();
								gg.setBiaoti(bt);
								gg.update((String)data.get(p1).get("id"), new UpdateListener(){

										@Override
										public void done(BmobException p1)
										{
											if(p1==null){
												gj.show("修改成功");
												du.dis();
												du.dis();
											}else{
												gj.show("修改失败,原因为"+p1);
												du.dis();
											}
											// TODO: Implement this method
										}
									});
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
				}
			});
		zujian.gg_nr.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View vv)
				{
					View[] v=du.dialoge(true,"修改公告内容","请输入内容");
					final EditText ed=(EditText) v[0];
					Button bt=(Button) v[1];
					ed.setText((String)data.get(p1).get("nr"));
					bt.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View vv)
							{
								du.dialogj1(false,"","加载中,请稍等");

								String bt=ed.getText().toString().trim();
								GongGao gg=new GongGao();
								gg.setNeirong(bt);
								gg.update((String)data.get(p1).get("id"), new UpdateListener(){

										@Override
										public void done(BmobException p1)
										{
											if(p1==null){
												gj.show("修改成功");
												du.dis();
												du.dis();
											}else{
												du.dis();
												gj.show("修改失败,原因为"+p1);
											}
											// TODO: Implement this method
										}
									});
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
				}
			});
		zujian.gg_delgg.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View vv)
				{
					View[] v=du.dialogt(false,"","确定该条删除公告？");
					Button b1,b2;
					b1=(Button) v[0];
					b2=(Button) v[1];
					b1.setText("取消");
					b2.setText("确定");
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});

					b2.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View vv)
							{
								du.dis();
								du.dialogj1(false,"","加载中,请稍等");
								GongGao gg=new GongGao();
								gg.delete((String)data.get(p1).get("id"), new UpdateListener(){

										@Override
										public void done(BmobException pp1)
										{
											if(pp1==null){
												gj.show("删除成功");
												delgg(p1);
											}else{
												gj.show("删除失败");
											}
											du.dis();
											// TODO: Implement this method
										}
									});
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
				}
			});
		return p2;
	}

}

